<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['jetLinks:devProduct:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['jetLinks:devProduct:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['jetLinks:devProduct:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['jetLinks:devProduct:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="devProductList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="设备类型: 网关，设备" align="center" prop="deviceType" />
      <el-table-column label="协议配置" align="center" prop="configuration" />
      <el-table-column label="创建者id" align="center" prop="creatorId" />
      <el-table-column label="所属机构id" align="center" prop="orgId" />
      <el-table-column label="数据存储策略" align="center" prop="storePolicy" />
      <el-table-column label="消息协议: Alink,JetLinks" align="center" prop="messageProtocol" />
      <el-table-column label="图片地址" align="center" prop="photoUrl" />
      <el-table-column label="分类名称" align="center" prop="classifiedName" />
      <el-table-column label="分类ID" align="center" prop="classifiedId" />
      <el-table-column label="消息协议名称" align="center" prop="protocolName" />
      <el-table-column label="传输协议: MQTT,COAP,UDP" align="center" prop="transportProtocol" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="说明" align="center" prop="describe" />
      <el-table-column label="说明" align="center" prop="id" />
      <el-table-column label="产品状态" align="center" prop="state" />
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="所属项目" align="center" prop="projectId" />
      <el-table-column label="数据存储策略配置" align="center" prop="storePolicyConfiguration" />
      <el-table-column label="入网方式: 直连,组网..." align="center" prop="networkWay" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['jetLinks:devProduct:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['jetLinks:devProduct:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改产品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="产品名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item  label="产品分类">
          <el-popover
            placement="bottom-start"
            width="460"
            trigger="click"
            @show="$refs['iconSelect'].reset()"
          >
            <DeviceType ref="iconSelect" @selected="selected" />
            <el-input slot="reference" v-model="form.classifiedName" placeholder="点击产品分类" readonly></el-input>
          </el-popover>
        </el-form-item>
        <el-form-item label="所属机构" prop="orgId">
          <el-input v-model="form.orgId" placeholder="请输入所属机构" />
        </el-form-item>


         <el-form-item label="消息协议" prop="messageProtocol">
          <el-select v-model="form.messageProtocol" placeholder="请选择">
            <el-option
              v-for="dict in protocolOptions"
              :key="dict.id"
              :label="dict.name"
              :value="dict.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="存储策略" prop="storePolicyConfiguration">
          <el-select v-model="form.storePolicyConfiguration" placeholder="请选择">
            <el-option
              v-for="dict in storePolicyOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备类型" prop="deviceType">
          <el-radio-group v-model="form.deviceType" size="medium">
            <el-radio v-for="(item, index) in deviceTypeOptions" :key="index" :label="item.dictValue"
              :disabled="item.disabled">{{item.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="说明" prop="describe">
          <el-input v-model="form.describe" type="textarea" placeholder="请输入说明"
            :autosize="{minRows: 1, maxRows: 4}" :style="{width: '100%'}"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDevProduct, getDevProduct, delDevProduct, addDevProduct, updateDevProduct, exportDevProduct } from "@/api/jetLinks/devProduct";
import { listProtocolAll } from "@/api/jetLinks/devProtocol";
import ImageUpload from '@/components/ImageUpload';
import { treeselect as menuTreeselect, roleMenuTreeselect } from "@/api/system/menu";
import DeviceType from "./deviceType";
export default {
  name: "DevProduct",
  components: {
    ImageUpload,
    DeviceType
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品表格数据
      devProductList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deviceType: null,
        metadata: null,
        configuration: null,
        creatorId: null,
        orgId: null,
        storePolicy: null,
        messageProtocol: null,
        photoUrl: null,
        classifiedName: null,
        classifiedId: null,
        protocolName: null,
        transportProtocol: null,
        name: null,
        describe: null,
        state: null,
        projectName: null,
        projectId: null,
        storePolicyConfiguration: null,
        networkWay: null
      },
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: "产品名称不能为空", trigger: "blur" }
        ],
      },
      // 设备类型字典
      deviceTypeOptions: [],
      // 存储策略字典
      storePolicyOptions:[],
      // 产品分类列表
      menuOptions: [],
      // 消息列表
      protocolOptions: [],
      messageProtocolAll: false,
    };
  },
  created() {
    this.getList();
    this.getDicts("equipment_type").then(response => {
      this.deviceTypeOptions = response.data;
    });
    this.getDicts("storage_strategy").then(response => {
      this.storePolicyOptions = response.data;
    });
    this.queryProtocolAll();
  },
  methods: {
    /** 查询产品列表 */
    getList() {
      this.loading = true;
      listDevProduct(this.queryParams).then(response => {
        this.devProductList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    queryProtocolAll(){
      listProtocolAll({"state":1}).then(response =>{
        this.protocolOptions = response.data;
      })
    },

    /** 查询菜单树结构 */
    getMenuTreeselect() {
      menuTreeselect().then(response => {
        this.menuOptions = response.data;
      });
    },
    /** 根据角色ID查询菜单树结构 */
    getRoleMenuTreeselect(roleId) {
      return roleMenuTreeselect(roleId).then(response => {
        this.menuOptions = response.menus;
        return response;
      });
    },
    onStrictly(){
      this.messageProtocolAll= true;
    },
    // 选择图标
    selected(data) {
      if(data){
        this.form.classifiedName = data.label;
        this.form.classifiedId = data.id;
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        deviceType: null,
        metadata: null,
        configuration: null,
        creatorId: null,
        orgId: null,
        storePolicy: null,
        messageProtocol: null,
        photoUrl: null,
        classifiedName: null,
        classifiedId: null,
        createTime: null,
        protocolName: null,
        transportProtocol: null,
        name: null,
        describe: null,
        id: null,
        state: null,
        projectName: null,
        projectId: null,
        storePolicyConfiguration: null,
        networkWay: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加产品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // this.reset();
      // const id = row.id || this.ids
      // getDevProduct(id).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改产品";
      // });
      const tableId = row.tableId || this.ids[0];
      this.$router.push({ path: '/devProduct/edit/index', query: { tableId: tableId} });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDevProduct(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDevProduct(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除产品编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delDevProduct(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('jetLinks/devProduct/export', {
        ...this.queryParams
      }, `devProduct_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
